package com.sihai.ehr.web.action.authority;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AuditType;
import com.sihai.ehr.model.biz.Auditing;
import com.sihai.ehr.model.biz.EmployeeinfoBase;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.AuditService;
import com.sihai.ehr.service.EmployeeService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.AuditConfig;
import com.sihai.ehr.web.pojo.StaffSearchPojo;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wxn
 * @since: 12-7-11 下午9:27
 *
 * 权限相关Action
 */

@Results({
        @Result(name = "main", location = "main_tab.jsp"),
        @Result(name = "audit_config_list", location = "audit_config_list.jsp"),
        @Result(name = "audit_config_set", location = "audit_config_set.jsp"),
        @Result(name = "audit_config_detail", location = "audit_config_detail.jsp")
})
public class AuditAction extends EHRAction {
    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private AuditService auditService;
    @BeanInject
    private EmployeeService employeeService;


    private List<Organization> topOrganizations; //顶级部门
    private List<AuditConfig> auditConfigs = new ArrayList<AuditConfig>();

    private AuditType auditType; //配置的当前审核
    private List<Auditing> auditings;

    private List<Organization> organizations;
    private Organization topOrg;

    private Long tid;
    private String batchIds;
    /** 需要审批流程的目标名称 */
    private String targetNames;

    @Action("/authority/audit_config")
    public String main() throws Exception {
        listAction = "/authority/audit_config_list.xhtml";
        listTitle = "审核配置";
        User user = getCurrentUser();
        if(user != null && user.getId() != null && user.getId() > 0){
            topOrganizations = new ArrayList<Organization>(1);
            Organization top = (user.getWork().getOrganization().getTopOrganization() == null ? user.getWork().getOrganization() : user.getWork().getOrganization().getTopOrganization());
            topOrganizations.add(top);
            organizationId = top.getId();
        }else{
            topOrganizations = organizationService.findChildren((Long)null, false, null, null);
            if(topOrganizations != null && !topOrganizations.isEmpty()){
                organizationId = topOrganizations.get(0).getId();
            }
        }
        return "main";
    }

    @Action("/authority/audit_config_list")
    public String list() throws Exception {
        AuditType[] audits = AuditType.values();
        for(AuditType at : audits){
            //add.wxn#2013-06-13:审核考核流程和定性考核对象不显示
            if(at == AuditType.audit_exam_config || at == AuditType.audit_exam_qual_target){
                continue;
            }
            Long time = auditService.findAuditFlowCount(organizationId, at);
            auditConfigs.add(new AuditConfig(at, time));
        }

        return "audit_config_list";
    }

    @Action("/authority/audit_config_set")
    public String set() throws Exception {
        if(auditType != null){
            //modify.wxn:2012.10.17#定性审核对象设置batchIds=7,5
            if(tid != null && tid > 0){//通过指定审核类型＋指定ID查找审核配置
                auditings = auditService.findAudits(organizationId, auditType, tid);
            }else{
                auditings = auditService.findAudits(organizationId, auditType);
            }

            organizations = organizationService.findChildren(organizationId, false, null, null);
            topOrg = organizationService.get(organizationId);
        }
        
        // modified by henry #start ---> cancel
//        if(batchIds != null && !batchIds.isEmpty()){ 
        	targetNames = getParameter("tn") != null && !getParameter("tn").isEmpty() ? getParameter("tn") : null;
//        }
        // modified by henry #end
        return "audit_config_set";
    }

    @Action("/authority/audit_config_detail")
    public String detail()throws Exception {
        if (auditType != null) {
            auditings = auditService.findAudits(organizationId, auditType);
            topOrg = organizationService.get(organizationId);
        }
        return "audit_config_detail";
    }

    public List<Organization> getTopOrganizations() {
        return topOrganizations;
    }

    public void setTopOrganizations(List<Organization> topOrganizations) {
        this.topOrganizations = topOrganizations;
    }

    public List<AuditConfig> getAuditConfigs() {
        return auditConfigs;
    }

    public AuditType getAuditType() {
        return auditType;
    }

    public void setAuditType(AuditType auditType) {
        this.auditType = auditType;
    }

    public List<Auditing> getAuditings() {
        return auditings;
    }

    public void setAuditings(List<Auditing> auditings) {
        this.auditings = auditings;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }

    public Organization getTopOrg() {
        return topOrg;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public String getBatchIds() {
        return batchIds;
    }

    public void setBatchIds(String batchIds) {
        this.batchIds = batchIds;
    }
    
    public String getTargetNames() {
		return targetNames;
	}
    
    public void setTargetNames(String targetNames) {
		this.targetNames = targetNames;
	}
}
